<template>
  <div class="xtx-category-page">
    <div class="container">
      <!-- 面包屑导航 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <!-- 动画在fade-right写好了 -->
        <Transition name="fade-right" mode="out-in">
          <XtxBreadItem :key="category.id">{{ category.name }}</XtxBreadItem>
        </Transition>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="height: 500px" />
      <!-- 全部分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in category.children" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{item.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类商品 -->
      <div class="ref-goods" v-for="item in category.children" :key="item.id">
        <div class="head">
          <h3>- {{item.name}} -</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="i in item.goods" :key="i.id" :goods="i" />
        </div>
      </div>

      <!-- 分类商品 over -->

    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { getBannerAPI } from '@/api/category'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import { getTopCategory } from '@/api/goods'
// import { useLazyDateFn } from '@/utils/hooks'

export default {
  name: 'xtx-category-page',
  setup() {
    const sliders = ref([])

    // 轮播图获取函数
    const initSliders = async () => {
      const data = await getBannerAPI()
      sliders.value = data.result
      // console.log('嘿嘿嘿')
      sliders.value = data.result.sort(() => Math.random() - 0.5)
    }

    // 页面生成
    onMounted(initSliders)

    //路由跳转之前
    onBeforeRouteUpdate((to, from, next) => {
      initSliders()
      next()
    })
    // 轮播图获取函数over

    // 获取分类信息
    const category = ref({})
    const initCategory = async id => {
      const data = await getTopCategory(id)
      category.value = data.result
      // console.log(data)
    }
    const route = useRoute()
    onMounted(() => {
      initCategory(route.params.id)
    })
    onBeforeRouteUpdate((to, from, next) => {
      initCategory(to.params.id)
      next()
    })
    // 获取分类信息over

    // 进行数据渲染
    // 需要知道商品数据的结构,传递商品对象给单个商品组件
    // 分类页面--全部分类--分类商品
    return { sliders, category }
  }
}
</script>

<style  scoped lang='less'>
.xtx-category-page {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: var(--xtx-color);
          }
        }
      }
    }
  }
}
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
    .none {
      height: 220px;
      text-align: center;
      width: 100%;
      line-height: 220px;
      color: #999;
    }
  }
}
</style>